﻿using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.XPath;

namespace WMSSystem
{
    public class AppServiceCommentOperationFilter : IOperationFilter
    {
        private readonly XPathDocument _xmlDoc;

        public AppServiceCommentOperationFilter(string xmlPath)
        {
            _xmlDoc = new XPathDocument(xmlPath);
        }

        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            // 获取服务层方法
            var methodInfo = context.MethodInfo;
            if (methodInfo == null) return;

            // 获取 XML 注释
            var memberName = $"M:{methodInfo.DeclaringType.FullName}.{methodInfo.Name}";
            var paramTypes = methodInfo.GetParameters();
            if (paramTypes.Length > 0)
            {
                memberName += $"({string.Join(",", paramTypes.Select(p => p.ParameterType.FullName))})";
            }

            var nav = _xmlDoc.CreateNavigator();
            var node = nav.SelectSingleNode($"/doc/members/member[@name='{memberName}']/summary");
            if (node != null)
            {
                operation.Summary = node.InnerXml.Trim();
            }
        }


    }
}
